package com.tcp2;

import com.domain.CarDevice;
import com.tcp.Sql;
import com.util.JDBCUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

class TCPThreadServerSocket implements Runnable {
    private Connection conn = null;
    private ServerSocket ss = null;
    private InputStream in = null;
    private OutputStream out = null;
    private Socket socket = null ;
    public TCPThreadServerSocket(ServerSocket ss) {
        this.ss = ss;
    }

    @Override
    public void run() {
        try {
            socket = ss.accept();
            this.conn = JDBCUtil.getConnection();
            //获取服务端输入的消息
            in = socket.getInputStream();
            //显示时间
            Date day=new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(df.format(day));
            //服务端返回的消息
            out = socket.getOutputStream();
            //用一个字节数字来存放消息，提高效率
            byte[] recData = new byte[1024];
            int length=in.read(recData);
            String hc=new String(recData, 0, length);
            System.out.println("读取到客户端发送来的数据：" + hc);

            if(hc.startsWith("HY:")){
                element(hc);
            }
            //返回给客户端的消息
            out.write("REG".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                //关闭资源
                in.close();
                out.close();
                //socket.close();
                TCPThreadServer.callback();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void element(String hc){
        CarDevice carDevice=new CarDevice();
        //去掉前三个字符
        hc=hc.substring(3);
        System.out.println("截取字符:" + hc);
        //里程总量
        int kilometers=50000;
        //根据,拆分
        String[] hcs=hc.split(",");
        carDevice.setDevice(hcs[0]);
        carDevice.setAp(hcs[1]);
        char ap=carDevice.getAp().charAt(0);
        //去掉数字前面的-号
        hc=carDevice.getAp().substring(1);
        float n=Integer.parseInt(hc);
        n=n-200;
        System.out.println(hc);
        //气压为正数时固定为100
        if(ap=='+'){
            carDevice.setAp("0");
            carDevice.setKilometers("0");
            //气压为负数时计算百分比
        }else if(n<=6200){
            float i=6200;
            float a=(float)(Math.round(n/i*100*100))/100;
            carDevice.setAp(a+"");
            System.out.println(a);
            //强转为int
            int s= (int) (kilometers-kilometers*a*0.8/100);
            System.out.println(s);
            //预估剩余里程
            carDevice.setKilometers(s+"");
        }else{
            carDevice.setAp("100");
            carDevice.setKilometers("48000");
        }
        if(Sql.select(carDevice.getDevice(),conn).equals("")){
            Sql.insert(carDevice,conn);
            System.out.println("insert");
        }else{
            Sql.update(carDevice,conn);
            System.out.println("update");
        }
    }

}

